package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("strategy")
@Controller
public class StrategyController {


    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyTemplateService strategyTemplateService;


    //
    @RequestMapping("")
    public Object index(Model model) {

        //国内外所有推荐VO
        List<StrategyStatisVO> voList = strategyStatisVORedisService.listCommendVO();
        //abroadCds  海外攻略
        List<StrategyStatisVO>  abroadCds = new ArrayList<>();
        //unabroadCds 国内排行
        List<StrategyStatisVO>  unabroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : voList) {
            if (vo.isIsabroad()){
                //国外
                if (abroadCds.size()<10){
                    abroadCds.add(vo);
                }
            }else {
                //国内
                if (unabroadCds.size()<10){
                    unabroadCds.add(vo);
                }
            }
        }
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);
        //hotCds 热门攻略
        model.addAttribute("hotCds",strategyStatisVORedisService.queryHotVOTop10());

        //themeCds 主题推荐
        List<Map<String,Object>> list = strategyTemplateService.queryThemeTop10();
        model.addAttribute("themeCds",list);

        //commends
        model.addAttribute("commends",strategyCommendService.getTop5());

        //chinas  国内
        List<StatisVO> chinas = strategyTemplateService.queryConditionProvincesGroup();
        model.addAttribute("chinas",chinas);
        //abroads 国外
        List<StatisVO> abroads = strategyTemplateService.queryConditionAbroadsGroup();
        model.addAttribute("abroads",abroads);

        //themes 主题
        List<StatisVO> themes = strategyTemplateService.queryConditionThemeGroup();
        model.addAttribute("themes",themes);

        return "strategy/index";
    }


    //首页条件查询
    @RequestMapping("/searchPage")
    public  String searchPage(Model model, @ModelAttribute("qo")StrategySearchQuery qo){


        //page
        model.addAttribute("page", strategyTemplateService.query(qo));

        return "strategy/searchPageTpl";
    }




    @RequestMapping("detail")
    public Object detail(Model model, Long id) {

        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail", detail);
        //阅读数加一
        strategyStatisVORedisService.viewnumIncrease(id,1);
        //加分
        strategyStatisVORedisService.addHotScore(id,1);
        StrategyStatisVO vo = strategyStatisVORedisService.getStrategyStatisVo(id);
        model.addAttribute("vo",vo);

        return "strategy/detail";
    }


    //旅游攻略
    @RequestMapping("list")
    public Object list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo) {

        //吐司操作
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //查询所有标签
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags",tags);

        model.addAttribute("pageInfo",strategyDetailService.query(qo));

        return "strategy/list";

    }

    //攻略评论添加
    @RequestMapping("commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){

        if (userInfo==null){
            throw new LogicException("请先登录");
        }

        comment.setCity(userInfo.getCity());
        comment.setCreateTime(new Date());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUsername(userInfo.getNickname());
        comment.setLevel(userInfo.getLevel());

        strategyCommentService.save(comment);

        //评论数加一
        strategyStatisVORedisService.replynumIncrease(comment.getDetailId(),1);

        //热门攻略加分
        strategyStatisVORedisService.addHotScore(comment.getDetailId(),1);

        int replynum = strategyStatisVORedisService.getStrategyStatisVo(comment.getDetailId()).getReplynum();

        return AjaxResult.SUCCESS.addData(replynum);
    }

    //评论列表
    @RequestMapping("comment")
    public String comment(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){

        Page page = strategyCommentService.query(qo);

        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }




    //点赞 commentThumbUp
    @RequestMapping("commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){
        strategyCommentService.commentThumbUp(toid,fromid);
        return AjaxResult.SUCCESS;
    }


    //攻略收藏
    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){

        if (userInfo ==null){
           AjaxResult ret =  new AjaxResult(false,"请先登录");
           ret.setCode(102);
           return ret;
        }
        //true 收藏 false 取消
        boolean flag = strategyStatisVORedisService.favor(sid, userInfo.getId());

        //加分减分
        strategyStatisVORedisService.addCommendScore(sid,flag?1:-1);


        return new AjaxResult(flag,null).addData(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }



    //strategyThumbup 点赞（顶）
    @RequestMapping("strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){

        if (userInfo ==null){
            AjaxResult ret =  new AjaxResult(false,"请先登录");
            ret.setCode(102);
            return ret;
        }
        //true 收藏 false 取消
        boolean flag = strategyStatisVORedisService.strategyThumbup(sid, userInfo.getId());
        //加分
        if (flag){
        strategyStatisVORedisService.addCommendScore(sid,1);
        }
        return new AjaxResult(flag,null).addData(strategyStatisVORedisService.getStrategyStatisVo(sid).getThumbsupnum());
    }



}
